﻿using Azylee.Core.DataUtils.CollectionUtils;
using Azylee.Core.DelegateUtils.ProcessDelegateUtils;
using Azylee.Core.IOUtils.FileUtils;
using Azylee.WinformSkin.FormUI.Toast;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JustMetClientTest2
{
    public partial class Form1 : Form
    {

        private List<string> files = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }


        #region 配置缓存路径

        /// <summary>
        /// 选择文件夹路径
        /// </summary>
        /// <returns></returns>
        private string SelectPath()
        {
            string path = string.Empty;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                path = fbd.SelectedPath;
            }
            return path;
        }

        /// <summary>
        /// 选择文件（可选多个）
        /// </summary>
        /// <returns></returns>
        private void SelectFile()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "请选择要加密的文件";
            fileDialog.Filter = "所有文件|*.*";
            fileDialog.Multiselect = true;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                if (ListTool.HasElements(fileDialog.FileNames))
                    foreach (var file in fileDialog.FileNames)
                    {
                        if (!files.Contains(file))
                        {
                            DgvFileEncryptList.Rows.Add(new object[] { file, "准备" });
                            files.Add(file);
                        }
                        else
                        {
                            ShowToast("文件已存在列表中，无需再次添加");
                        }

                    }
            }
        }


        #endregion

        #region 文件拖拽
        private void DgvFileEncryptList_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void DgvFileEncryptList_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (!files.Contains(path))
            {
                if (FileTool.IsFile(path))
                {
                    DgvFileEncryptList.Rows.Add(new object[] { path, "准备" });
                    files.Add(path);
                }
                else
                {
                    ShowToast("暂时不支持加密文件夹，请选择您需要加密的文件");
                }
            }
            else
            {
                ShowToast("文件已存在列表中，无需再次添加");
            }
        }

        #endregion

        #region 提示框

        /// <summary>
        /// 提示框
        /// </summary>
        /// <param name="title"></param>
        private void ShowToast(String title)
        {
            this.Invoke(new Action(() =>
            {
                SmallToastForm.Display(title, 'i', 3000, null);
            }));
        }

        #endregion

        #region 点击事件


        /// <summary>
        /// 清空列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtFileEncryptClear_Click(object sender, EventArgs e)
        {
            DgvFileEncryptList.Rows.Clear();
        }

        /// <summary>
        /// 批量加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtFileEncrypt_Click(object sender, EventArgs e)
        {
            ((Button)sender).Enabled = false;
            string password = "71D1FD4326DA1B7B118203D699EDA85";
            Task.Factory.StartNew(() =>
            {
                int index = 0;
                foreach (DataGridViewRow row in DgvFileEncryptList.Rows)
                {
                    string file = row.Cells["CoFileEncryptName"].Value.ToString();
                    int flag;
                    UIFileEncryptStatus(index, "正在分析文件...");
                    if ((flag = FileEncryptTool.Encrypt(file, file + FileEncryptTool.FileExt, password, UIFileEncryptProgress, index)) > 0)
                    {
                        UIFileEncryptStatus(index, "加密成功");
                        if (CbFileEncryptDelete.Checked)
                        {
                            FileTool.Delete(file);
                        }
                    }
                    else
                    {
                        UIFileEncryptStatus(index, "加密失败:" + flag);
                    }
                    index++;
                }
                BeginInvoke(new Action(() => { ((Button)sender).Enabled = true; }));
            });
        }

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnFileSelect_Click(object sender, EventArgs e)
        {
            SelectFile();
        }

        #endregion

        #region 更新状态

        /// <summary>
        /// 更新文件状态
        /// </summary>
        /// <param name="index"></param>
        /// <param name="status"></param>
        private void UIFileEncryptStatus(int index, string status)
        {
            BeginInvoke(new Action(() =>
            {
                DgvFileEncryptList.Rows[index].Cells["CoFileEncryptStatus"].Value = status;
            }));
        }

        /// <summary>
        /// 显示当前加密的文件进度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UIFileEncryptProgress(object sender, ProgressEventArgs e)
        {
            BeginInvoke(new Action(() =>
            {
                DgvFileEncryptList.Rows[(int)sender].Cells["CoFileEncryptStatus"].Value = "加密中："+
                (e.Current * 100 / e.Total) + " %";
            }));
        }
        #endregion

        #region 保存到txt本地数据

        private void SaveCompleteData(String path)
        {

        }

        #endregion

    }
}
